package com.canbot.u05.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.canbot.u05.R;

public class KeyboardLayout extends RelativeLayout implements View.OnClickListener {
    private static final String TAG = "KeyboardLayout";
    private OnkeyBoardClickListener listener;
    private static final String COMPLETE_KEY = "complete";
    private View btnSure;

    public KeyboardLayout(Context context) {
        super(context);
    }

    public KeyboardLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public KeyboardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

    }

    public void setOnkeyBoardClickListener(OnkeyBoardClickListener listener){

        this.listener = listener;
    }

    public void setCompleteEnable(Boolean enable){

        Log.e(TAG,"setCompleteEnable:" + enable);

        if (btnSure != null){
            btnSure.setClickable(enable);
            if (enable){
                btnSure.setBackgroundResource(R.drawable.shape_keyboard_btn_sure_selector);
            }else{
                btnSure.setBackgroundResource(R.drawable.shape_keyboard_btn_sure_unclickable);
            }
        }

    }


    @Override
    protected void onWindowVisibilityChanged(int visibility)
    {
        super.onWindowVisibilityChanged(visibility);

        if (visibility == VISIBLE){         //view可见

            int childCount = getChildCount();
            Log.e(TAG,"childCount:" + childCount);
            for (int i = 0; i < childCount; i++) {

                getChildAt(i).setOnClickListener(this);
                if (getChildAt(i).getTag().equals(COMPLETE_KEY)){
                    btnSure = getChildAt(i);
                    btnSure.setClickable(false); //设置为默认不可点击
                }


            }


        }else if(visibility == INVISIBLE || visibility == GONE){    //view不可见

        }
    }

    @Override
    public void onClick(View v) {
        if (listener != null){

            String key = (String) v.getTag();

            listener.onkeyBoadrClick(key);


        }
    }

    interface OnkeyBoardClickListener {
        void onkeyBoadrClick(String key);
    }
}
